﻿using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.Infrastructure.WPF.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;
using SlamDunk.MES.IAppService.DTOs.Outputs.ConfigurationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Client.ViewModels.Schedule
{
    /// <summary>
    /// 通用弹出层-工单信息ViewModel
    /// </summary>
    public class SelectWorkOrderViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 库存单据服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public SelectWorkOrderViewModel(ServiceProxy<IExecutionContract> iExecutionContract)
        {
            this._iExecutionContract = iExecutionContract;
            //默认值
            this.PageIndex = 1;
            this.PageSize = 20;
        }

        #endregion

        #region # 属性

        #region 页码 —— int PageIndex
        /// <summary>
        /// 页码
        /// </summary>
        [DependencyProperty]
        public int PageIndex { get; set; }
        #endregion

        #region 页容量 —— int PageSize
        /// <summary>
        /// 页容量
        /// </summary>
        [DependencyProperty]
        public int PageSize { get; set; }
        #endregion

        #region 总记录数 —— int RowCount
        /// <summary>
        /// 总记录数
        /// </summary>
        [DependencyProperty]
        public int RowCount { get; set; }
        #endregion

        #region 总页数 —— int PageCount
        /// <summary>
        /// 总页数
        /// </summary>
        [DependencyProperty]
        public int PageCount { get; set; }
        #endregion

        #region 关键字
        /// <summary>
        /// 关键字
        /// </summary>
        [DependencyProperty]
        public string Keywords { get; set; }
        #endregion

        #region 开始时间
        /// <summary>
        /// 开始时间
        /// </summary>
        [DependencyProperty]
        public DateTime? StartTime { get; set; }
        #endregion

        #region 结束时间
        /// <summary>
        /// 结束时间
        /// </summary>
        [DependencyProperty]
        public DateTime? EndTime { get; set; }
        #endregion

        #region 工单列表
        /// <summary>
        /// 工单列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Wrap<WorkOrderInfo>> WorkOrders { get; set; }
        #endregion

        #region 工单列表
        /// <summary>
        /// 工单列表
        /// </summary>
        [DependencyProperty]
        public List<WorkOrderStatus> WorkOrderStatusList { get; set; }
        #endregion

        #region 是否多选
        /// <summary>
        /// 是否多选
        /// </summary>
        [DependencyProperty]
        public bool IsCheckbox { get; set; }
        #endregion
        #endregion

        #region # 方法
        //Initializations
        #region 加载 —— async Task Load(List<WorkOrderStatus> workOrderStatusList)
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="workOrderStatusList">工单状态-</param>
        /// <returns></returns>
        public async Task Load(List<WorkOrderStatus> workOrderStatusList, bool isCheckbox)
        {
            this.WorkOrderStatusList = workOrderStatusList;
            this.IsCheckbox = isCheckbox;
            await this.ReloadWorkOrders();
        }
        #endregion

        //Actions

        #region 加载工单列表 —— async void LoadWorkOrders()
        /// <summary>
        /// 加载工单列表
        /// </summary>
        public async void LoadWorkOrders()
        {
            await this.ReloadWorkOrders();
        }
        #endregion

        #region 全选 —— void CheckAll()
        /// <summary>
        /// 全选
        /// </summary>
        public void CheckAll()
        {
            this.WorkOrders.ForEach(x => x.IsChecked = true);
        }
        #endregion

        #region 取消全选 —— void UncheckAll()
        /// <summary>
        /// 取消全选
        /// </summary>
        public void UncheckAll()
        {
            this.WorkOrders.ForEach(x => x.IsChecked = false);
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证
            List<WorkOrderInfo> checkedRoles = this.WorkOrders.Where(x => x.IsChecked == true).Select(x => x.Model).ToList();
            if (!IsCheckbox)
            {
                if (!checkedRoles.Any() || checkedRoles.Count() != 1)
                {
                    MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            else
            {
                if (!checkedRoles.Any())
                {
                    MessageBox.Show("请至少勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            #endregion
            await base.TryCloseAsync(true);
        }
        #endregion
        //Private

        #region 加载工单列表 —— async Task ReloadWorkOrders()
        /// <summary>
        /// 加载工单班组列表
        /// </summary>
        private async Task ReloadWorkOrders()
        {
            this.Busy();
            UserExtensionInfo userExtensionInfo = (UserExtensionInfo)AppDomain.CurrentDomain.GetData(Constants.UserExtensionKey);
            PageModel<WorkOrderInfo> pageModel = await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrdersByPage(
                Keywords,//关键字
                null,//生产计划Id
                null, //作业计划Id
                null, //工艺段Id
                null, //工位Id
                null, //是否需质检
                null,//是否已质检
                userExtensionInfo?.TeamInfo?.Id,//分派班组Id
                null,//分派人员Id
                StartTime, //开始时间
                EndTime, //结束时间
                null, //工厂模型Id
                WorkOrderStatusList, //工单状态集
                this.PageIndex,//页码
                this.PageSize));
            this.RowCount = pageModel.RowCount;
            this.PageCount = pageModel.PageCount;

            IEnumerable<Wrap<WorkOrderInfo>> wrapModels = pageModel.Datas.Select(x => x.Wrap());
            this.WorkOrders = new ObservableCollection<Wrap<WorkOrderInfo>>(wrapModels);

            this.Idle();
        }
        #endregion


        #endregion
    }
}
